package com.newsystem.controller;


import com.newsystem.MyUtils.CommonResult;
import com.newsystem.mapper.CollectionMapper;
import com.newsystem.mapper.CommentMapper;
import com.newsystem.mapper.TGoodMapper;
import com.newsystem.service.impl.CollectionService;
import com.newsystem.service.impl.CommentService;
import com.newsystem.service.impl.TGoodService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 谁收藏什么新闻
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@RestController
@RequestMapping("/collection")
@RequiredArgsConstructor
public class CollectionController {

    private final CollectionService collectionService;
    private final CollectionMapper collectionMapper;

    @ApiOperation("用户收藏新闻或者取消收藏")
    @PutMapping("/addCollection")
    public CommonResult<Object> addCollection(String userId, String newsId){
        return collectionService.insertCollection(userId,newsId);
    }


    @ApiOperation("用户获取自己的收藏的新闻列表")
    @GetMapping("/getCollections")
    public CommonResult<Object> getCollections(String userId){
        List<Map<String, Object>> maps = collectionMapper.selectByUserId(userId);
        if(maps==null){
            return CommonResult.failed("获取失败");
        }
        else {
            return CommonResult.success(maps);
        }
    }

    @ApiOperation("用户通过收藏id取消收藏的新闻")
    @DeleteMapping("/cancel")
    public CommonResult<Object> cancelCollection(String userId,String collectionId){
        return collectionService.deleteCollection(userId,collectionId);
    }


}

